﻿Public Class frm_host

    Private Sub frm_host_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frm_main.ListBox1.Items.Add(TimeOfDay & " - Volviendo a interfaz principal")
        frm_main.Show()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargardatos(GetSetting(Application.ProductName, "config", "gamepath") & "\")
    End Sub
    Private Sub TextBox2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Click
        TextBox2.Text = ""
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        NumerosyDecimal2(TextBox2, e)
    End Sub

    Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
        If TextBox2.Text = "" Then
            TextBox2.Text = "300"
        End If
    End Sub
    Public Function cargardatos(ByVal carpeta As String)
        Dim cantidad, total, nombre
        frm_main.ListBox1.Items.Add(TimeOfDay & " - Buscando mapas...")
        cantidad = My.Computer.FileSystem.GetFiles(carpeta, FileIO.SearchOption.SearchAllSubDirectories, "mp*.bsp").Count
        Try
            If cantidad = 0 Then
                frm_main.ListBox1.Items.Add(TimeOfDay & " - NO SE ENCONTRO NINGUN MAPA")
                ListBox1.Items.Add("NO SE ENCONTRO NINGUN MAPA")
                ListBox1.Items.Add("Comprueba  que los mapas esten instalados.")
                ListBox1.Enabled = False
                TextBox2.Enabled = False
                Button1.Enabled = False
            Else
                frm_main.ListBox1.Items.Add(TimeOfDay & " - Cantidad de mapas encontrados..." & cantidad)
                Dim medio = 0
                For Each recent As String In _
                    My.Computer.FileSystem.GetFiles(carpeta, _
                                                    FileIO.SearchOption.SearchAllSubDirectories, "mp*.bsp")
                    total = Len(My.Computer.FileSystem.GetFileInfo(recent).Name)
                    nombre = Mid(My.Computer.FileSystem.GetFileInfo(recent).Name, 1, total - 4)
                    ListBox1.Items.Add(nombre)
                    If Me.Focused = True Then
                        Me.Update()
                    End If
                    Application.DoEvents()
                Next
                frm_main.ListBox1.Items.Add(TimeOfDay & " - Se agregaron " & ListBox1.Items.Count & " a la lista")
            End If
        Catch errores As Exception
            MsgBox(errores.Message, MsgBoxStyle.Critical)
            frm_main.ListBox1.Items.Add(TimeOfDay & " - Error critico..." & errores.Message)
        End Try
        Return 0
    End Function
    Public Sub NumerosyDecimal2(ByVal CajaTexto As Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim carpeta = GetSetting(Application.ProductName, "config", "gamepath")
        frm_main.ListBox1.Items.Add(TimeOfDay & " - Iniciando juego en modo Host...")
        frm_main.ListBox1.Items.Add(TimeOfDay & " - Mapa seleccionado '" & ListBox1.SelectedItem & "'")
        frm_main.ListBox1.Items.Add(TimeOfDay & " - Tiempo de espera por compañero " & TextBox2.Text & " segundos...")
        If CheckBox1.Checked = True Then
            Shell(carpeta & "\portal2.exe -novid +sv_lan 1 +mp_wait_for_other_player_notconnecting_timeout " & TextBox2.Text & " +map " & ListBox1.SelectedItem)
        Else
            Shell(carpeta & "\portal2.exe -novid +mp_wait_for_other_player_notconnecting_timeout " & TextBox2.Text & " +map " & ListBox1.SelectedItem)
        End If
    End Sub
End Class
